//
//  MonthYearView.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 1/4/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "MonthYearView.h"

@implementation MonthYearView

- (id)initWithMonth:(int)month year:(int)year
{
    self = [super init];
    if (self)
    {
        _selectedMonth = month;
        _selectedYear = year;
        
        [self initializeMonthView];
        [self initializeYearPickerView];
    }
    
    return self;
}

- (void)setFrame:(CGRect)frame
{
    frame = RECT(0, 0, SCREEN_SIZE.width, 216);
    [super setFrame:frame];
    
    _monthView.frame = RECT(0, 0, self.width/3*2, self.height);
    float left = 0;
    float top = 0;
    float wid = _monthView.width/3;
    float hei = _monthView.height/4;
    for (int i = 0; i < _monthView.subviews.count; i++)
    {
        left = (i % 3)*wid;
        top = ((int)i/3)*hei;
        
        UIButton *month = [_monthView.subviews objectAtIndex:i];
        month.frame = RECT(left, top, wid, hei);
    }
    
    _yearPickerView.frame = RECT(_monthView.right, 0, self.width - _monthView.right, self.height);
}

- (void)initializeMonthView
{
    _monthView = [[UIView alloc] init];
    [self addSubview:_monthView];
    
    for (int i = 1; i <= 12; i++)
    {
        UIButton *button = [UIButton buttonWithTitle:[NSString stringWithFormat:@"%d",i] target:self selector:@selector(monthPressed:)];
        button.tag = i;
        [button setTitleColor:COLOR_BLACK_PURE forState:UIControlStateNormal];
        [button setTitleColor:COLOR_WHITE_PURE forState:UIControlStateHighlighted];
        [button setTitleColor:COLOR_WHITE_PURE forState:UIControlStateSelected];
        button.layer.borderColor = COLOR_GRAY_LIGHT.CGColor;
        button.layer.borderWidth = 0.5f;
        [button setBackgroundImage:[UIImage imageFromColor:COLOR_WHITE_PURE] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageFromColor:COLOR_GRAY] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageFromColor:COLOR_GRAY] forState:UIControlStateSelected];
        [_monthView addSubview:button];
        
        if (i == _selectedMonth)
        {
            [button setSelected:YES];
        }
    }
}

- (void)initializeYearPickerView
{
    _minYear = 2000;
    _maxYear = 2020;
    _years = [[NSMutableArray alloc] init];
    
    for (int i = _minYear; i<= _maxYear; i++)
    {
        [_years addObject:[NSNumber numberWithInt:i]];
    }
    
    _yearPickerView = [[UIPickerView alloc] init];
    _yearPickerView.delegate = self;
    _yearPickerView.showsSelectionIndicator = YES;
    _yearPickerView.dataSource = self;
    _yearPickerView.backgroundColor = [UIColor clearColor];
    [self addSubview:_yearPickerView];
    
    [_yearPickerView selectRow:_selectedYear - _minYear inComponent:0 animated:NO];
}

- (void)highlightMonth:(UIButton*)hightlightedButton
{
    for (UIButton *button in _monthView.subviews)
    {
        [button setSelected:NO];
        
        if ([button isEqual:hightlightedButton])
        {
            [button setSelected:YES];
        }
    }
}

- (void)raiseEventSelectMonthYear
{
    if ([_delegate respondsToSelector:@selector(monthYearView:didSelectMonth:year:)])
    {
        [_delegate monthYearView:self didSelectMonth:_selectedMonth year:_selectedYear];
    }
}

- (void)monthPressed:(UIButton*)sender
{
    [self highlightMonth:sender];
    _selectedMonth = sender.tag;
    
    [self raiseEventSelectMonthYear];
}

#pragma mark - UIPicker delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _years.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return self.height/5;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%d",[[_years objectAtIndex:row] integerValue]];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _selectedYear = [[_years objectAtIndex:row] integerValue];
    
    [self raiseEventSelectMonthYear];
}

@end
